//
//  uiNodeManager.hpp
//  smu_test1
//
//  Created by jiayi on 16/9/25.
//
//

#ifndef uiNodeManager_hpp
#define uiNodeManager_hpp

class uiNode;
class smuNode;
class smuGraph;

// update node
class uiUpdateNode : public Ref, public uiFactoryItem
{
public:
    uiCreate( uiUpdateNode );
    
    virtual void deleteFactoryItem(){ updateType = -1; node = nullptr; }
    
    int updateType;
    uiNode* node;
    uiUpdateNode() : updateType(-1), node(nullptr){}
};

// Singleton class for loading .txt file
class uiNodeManager
{
public:
    // resources define
    typedef Map< string, uiNode* > uiNodeDatas;
    typedef uiNodeDatas::iterator uiNodeDatasIter;
    typedef uiNodeDatas::const_iterator uiNodeDatasConstIter;
    
    
    defVector( uiUpdateNode );
    
public:
    ~uiNodeManager();
    
    // initialize data
    int initializeData();
    
    // destroy data
    void destroyData();
    
    // save to file
    void saveData();
    
    // parse raw string to smuReaderData
    void processString( string& line, bool fake );
    
    // call when added all nodes
    void endAddNodes();
    
    // delete node
    void deleteOrRestoreNode( smuNode* node, bool del );
    
    // get datas
    const uiNodeDatas& getDatas() const { return _datas; }
    
    // get new datas
    uiUpdateNodes& getNewDatas() { return _newDatas; }
    
    // process when update finished
    void updateCallback( smuNode* node, int param );
    
    // get root node
    uiNode* getRootNode() const { return _root; }
    
private:
    // find or create a node and return it
    uiNode* _addNode( std::string name, bool fake );
    
    // add link
    void _addLink( uiNode* n1, uiNode* n2, bool eraseEmpty = true );
    
private:
    // resources
    uiNodeDatas _datas;
    uiUpdateNodes _newDatas;
    uiNodeDatas _emptyFromDatas;
    
    // root
    uiNode* _root;
    
    // graph
    smuGraph* _graph;
    
    
    // Singleton implement
public:
    static uiNodeManager& getInstance(){
        static uiNodeManager s;
        return s;
    }
    
protected:
    uiNodeManager() : _graph(0){}
};

#endif /* uiNodeManager_hpp */
